Skip to content

Return the updated inp from seteitem_ - #2397

Merged
t-vi merged 2 commits into
mainfrom
return-setitem_-output
Aug 14, 2025
Merged

t-vi merged 2 commits into
mainfrom
return-setitem_-output

Conversation

@crcrpar

@crcrpar crcrpar commented Aug 5, 2025

Copy link
Copy Markdown
Collaborator

What does this PR do?

Fixes the bug I see when running "qihoo360/360Zhinao-7B-Base" model using benchmark_peft.py:

Traceback (most recent call last):
  File "/workspace/cm/benchmark_peft.py", line 723, in <module>
    main(args)
  File "/workspace/cm/benchmark_peft.py", line 565, in main
    outputs = model(**batch)
              ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/_dynamo/eval_frame.py", line 372, in __call__
    return super().__call__(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py", line 1767, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py", line 1778, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/_dynamo/eval_frame.py", line 699, in compile_wrapper
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py", line 1767, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py", line 1778, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/peft/peft_model.py", line 1845, in forward
    return self.base_model(
           ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py", line 1767, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py", line 1778, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/peft/tuners/tuners_utils.py", line 216, in forward
    return self.model.forward(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/cm_hf_cache/modules/transformers_modules/qihoo360/360Zhinao-7B-Base/8c3ea4e52a4fe31f3c11de0a21f3ae21dad21dc6/modeling_zhinao.py", line 816, in forward
    outputs = self.model(
              ^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py", line 1767, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py", line 1778, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/cm_hf_cache/modules/transformers_modules/qihoo360/360Zhinao-7B-Base/8c3ea4e52a4fe31f3c11de0a21f3ae21dad21dc6/modeling_zhinao.py", line 711, in forward
    layer_outputs = decoder_layer(
                    ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py", line 1767, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py", line 1778, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/cm_hf_cache/modules/transformers_modules/qihoo360/360Zhinao-7B-Base/8c3ea4e52a4fe31f3c11de0a21f3ae21dad21dc6/modeling_zhinao.py", line 513, in forward
    hidden_states, self_attn_weights, present_key_value = self.self_attn(
                                                          ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py", line 1767, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py", line 1778, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/cm_hf_cache/modules/transformers_modules/qihoo360/360Zhinao-7B-Base/8c3ea4e52a4fe31f3c11de0a21f3ae21dad21dc6/modeling_zhinao.py", line 416, in forward
    attn_output = self.flash_attention(query_states, key_states, value_states, attention_mask) 
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/cm_hf_cache/modules/transformers_modules/qihoo360/360Zhinao-7B-Base/8c3ea4e52a4fe31f3c11de0a21f3ae21dad21dc6/modeling_zhinao.py", line 345, in flash_attention
    query_states, key_states, value_states, indices_q, cu_seq_lens, max_seq_lens = self._upad_input(
  File "/cm_hf_cache/modules/transformers_modules/qihoo360/360Zhinao-7B-Base/8c3ea4e52a4fe31f3c11de0a21f3ae21dad21dc6/modeling_zhinao.py", line 345, in torch_dynamo_resume_in_flash_attention_at_345
    query_states, key_states, value_states, indices_q, cu_seq_lens, max_seq_lens = self._upad_input(
  File "/usr/local/lib/python3.12/dist-packages/torch/_dynamo/eval_frame.py", line 372, in __call__
    return super().__call__(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py", line 1767, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py", line 1778, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/_dynamo/eval_frame.py", line 893, in _fn
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/fx/graph_module.py", line 840, in call_wrapped
    return self._wrapped_call(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/fx/graph_module.py", line 416, in __call__
    raise e
  File "/usr/local/lib/python3.12/dist-packages/torch/fx/graph_module.py", line 403, in __call__
    return super(self.cls, obj).__call__(*args, **kwargs)  # type: ignore[misc]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py", line 1767, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py", line 1778, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<eval_with_key>.87", line 6, in forward
  File "/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py", line 1767, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/torch/nn/modules/module.py", line 1778, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pytorch/lightning-thunder/thunder/core/module.py", line 80, in forward
    res = self._forward_fn(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pytorch/lightning-thunder/thunder/__init__.py", line 839, in wrapped
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/opt/pytorch/lightning-thunder/thunder/__init__.py", line 879, in fn_
    cache_entry, inps, pro_to_epi = get_computation_and_inputs(*args, **kwargs)
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pytorch/lightning-thunder/thunder/__init__.py", line 818, in wrapped
    cache_entry, inps, pro_to_epi = get_computation_and_inputs_fn(*args, **kwargs)
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pytorch/lightning-thunder/thunder/core/langctxs.py", line 135, in _fn
    result = fn(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^
  File "/opt/pytorch/lightning-thunder/thunder/__init__.py", line 238, in cache_info_wrapper
    res = fn(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^
  File "/opt/pytorch/lightning-thunder/thunder/__init__.py", line 784, in get_computation_and_inputs
    cache_entry = apply_transforms_and_build_cache_entry(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pytorch/lightning-thunder/thunder/__init__.py", line 566, in apply_transforms_and_build_cache_entry
    computation_trc = grad_transform_on_trace(computation_trc)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pytorch/lightning-thunder/thunder/transforms/autodiff.py", line 436, in grad_transform_on_trace
    joint_trace, _ = AugmentedForwardProcessor(trace)()
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pytorch/lightning-thunder/thunder/core/trace_interpreter.py", line 374, in __call__
    self.process_bsym(bsym)
  File "/opt/pytorch/lightning-thunder/thunder/transforms/autodiff.py", line 300, in process_bsym
    result = joint_forward_backward(*bsym.args, **bsym.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pytorch/lightning-thunder/thunder/core/jit_ext.py", line 1002, in grad_transform
    grads = tree_map(lambda t: get_grad(t), sequencify(primal))
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/optree/ops.py", line 766, in tree_map
    return treespec.unflatten(map(func, *flat_args))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pytorch/lightning-thunder/thunder/core/jit_ext.py", line 1002, in <lambda>
    grads = tree_map(lambda t: get_grad(t), sequencify(primal))
                               ^^^^^^^^^^^
  File "/opt/pytorch/lightning-thunder/thunder/core/symbol.py", line 312, in __call__
    result = self.meta(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pytorch/lightning-thunder/thunder/core/langctxs.py", line 135, in _fn
    result = fn(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^
  File "/opt/pytorch/lightning-thunder/thunder/core/prims.py", line 1834, in _get_grad_meta
    utils.check_type(a, (Number, NumberProxy, TensorProxy))
  File "/opt/pytorch/lightning-thunder/thunder/core/baseutils.py", line 161, in check_type
    check(
  File "/opt/pytorch/lightning-thunder/thunder/core/baseutils.py", line 157, in check
    raise exception_type(s())
ValueError: None had an unexpected type <class 'NoneType'>. Supported types are (<class 'numbers.Number'>, <class 'thunder.core.proxies.NumberProxy'>, <class 'thunder.core.proxies.TensorProxy'>)

The following trace

def higher_order_autograd_function_apply_brtuu(any5, t_0, t_1):
  # <eval_with_key>.83:5:           _set_grad_enabled = torch._C._set_grad_enabled(False);  _set_grad_enabled = None
  ltorch._set_grad_enabled_with_warning(False)

  # <eval_with_key>.83:6:           output = torch.zeros(4096, 32, 128, device = device(type='cuda', index=0), dtype = torch.bfloat16)
  t2 = ltorch.zeros(4096, 32, 128, device=devices.Device("cuda:0"), dtype=torch.bfloat16)  # t2: "cuda:0 bf16[4096, 32, 128] self._requires_grad=False"
    # t2 = ltorch.full((4096, 32, 128), 0, device=devices.Device("cuda:0"), dtype=torch.bfloat16)  # t2: "cuda:0 bf16[4096, 32, 128] self._requires_grad=False"
      # t2 = prims.full((4096, 32, 128), 0, device=devices.Device("cuda:0"), dtype=dtypes.bfloat16)  # t2: "cuda:0 bf16[4096, 32, 128] self._requires_grad=False"

  # <eval_with_key>.83:8:           _set_grad_enabled_1 = torch._C._set_grad_enabled(True);  _set_grad_enabled_1 = None
  (t0,) = prims.update_aliases((t2,))
  t4 = ltorch.setitem_(t0, t_1, t_0)  # t4: "cuda:0 bf16[4096, 32, 128] self._requires_grad=False"
    # t3 = ltorch.setitem(t0, t_1, t_0)  # t3: "cuda:0 bf16[4096, 32, 128] self._requires_grad=False"
      # t3 = prims.copy_with_setitem(t0, t_1, t_0)  # t3: "cuda:0 bf16[4096, 32, 128] self._requires_grad=False"
    # t4 = prims.copy_(t3, t0, grad_enabled=False)  # t4: "cuda:0 bf16[4096, 32, 128] self._requires_grad=False"
  ltorch._set_grad_enabled_with_warning(True)
  return (t4, [t_1])

seems to return None inside

def grad_transform(*args, **kwargs):
from thunder.core.transforms import get_grad, put_grads
primal, residuals = interpret_trace(aliased_aug_fwd_trace, *args, **kwargs)
grads = tree_map(lambda t: get_grad(t), sequencify(primal))
bwd_args = (None,) + tuple(grads) + tuple(sequencify(residuals))
result = interpret_trace(aliased_bwd_trace, *bwd_args)
put_grads(args[1:], result)
return primal
of _general_jit_torch_ops_higher_order_autograd_function_apply and the cause seems to be that ltorch.setitem_ has been a void function.

@crcrpar
crcrpar force-pushed the return-setitem_-output branch from 71a3b20 to fd2234e Compare August 6, 2025 05:00
@beverlylytle

beverlylytle commented Aug 6, 2025

Copy link
Copy Markdown
Collaborator

I added this which is the source of the bug, and it should also be removed with this PR.

The following trace
```
def higher_order_autograd_function_apply_brtuu(any5, t_0, t_1):
  # <eval_with_key>.83:5:           _set_grad_enabled = torch._C._set_grad_enabled(False);  _set_grad_enabled = None
  ltorch._set_grad_enabled_with_warning(False)

  # <eval_with_key>.83:6:           output = torch.zeros(4096, 32, 128, device = device(type='cuda', index=0), dtype = torch.bfloat16)
  t2 = ltorch.zeros(4096, 32, 128, device=devices.Device("cuda:0"), dtype=torch.bfloat16)  # t2: "cuda:0 bf16[4096, 32, 128] self._requires_grad=False"
    # t2 = ltorch.full((4096, 32, 128), 0, device=devices.Device("cuda:0"), dtype=torch.bfloat16)  # t2: "cuda:0 bf16[4096, 32, 128] self._requires_grad=False"
      # t2 = prims.full((4096, 32, 128), 0, device=devices.Device("cuda:0"), dtype=dtypes.bfloat16)  # t2: "cuda:0 bf16[4096, 32, 128] self._requires_grad=False"

  # <eval_with_key>.83:8:           _set_grad_enabled_1 = torch._C._set_grad_enabled(True);  _set_grad_enabled_1 = None
  (t0,) = prims.update_aliases((t2,))
  t4 = ltorch.setitem_(t0, t_1, t_0)  # t4: "cuda:0 bf16[4096, 32, 128] self._requires_grad=False"
    # t3 = ltorch.setitem(t0, t_1, t_0)  # t3: "cuda:0 bf16[4096, 32, 128] self._requires_grad=False"
      # t3 = prims.copy_with_setitem(t0, t_1, t_0)  # t3: "cuda:0 bf16[4096, 32, 128] self._requires_grad=False"
    # t4 = prims.copy_(t3, t0, grad_enabled=False)  # t4: "cuda:0 bf16[4096, 32, 128] self._requires_grad=False"
  ltorch._set_grad_enabled_with_warning(True)
  return (t4, [t_1])
```
seems to return `None` inside
https://github.com/Lightning-AI/lightning-thunder/blob/a7952e29bae925581d3bf7069d64386f4d888e00/thunder/core/jit_ext.py#L998-L1007
of `_general_jit_torch_ops_higher_order_autograd_function_apply` and the
cause seems to be that `ltorch.setitem_` has been a void function.

Signed-off-by: Masaki Kozuki <mkozuki@nvidia.com>
@crcrpar
crcrpar force-pushed the return-setitem_-output branch from fd2234e to 38d6bde Compare August 12, 2025 07:28

@t-vi t-vi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@t-vi
t-vi enabled auto-merge (squash) August 14, 2025 09:39
@t-vi
t-vi merged commit 45a2e93 into main Aug 14, 2025
54 of 65 checks passed
@t-vi
t-vi deleted the return-setitem_-output branch August 14, 2025 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants